package cn.uniqueinfo.dp.exception;

import cn.uniqueinfo.dp.enums.CtrlMethodEnum;
import lombok.Data;

import java.io.PrintWriter;
import java.io.StringWriter;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-04 14:40:26
 * @description:自定义异常
 */
@Data
public class UQException extends RuntimeException {

    /** 异常类型名称 */
    private String name;
    /** 异常信息 */
    private String msg;
    /** 异常编码 */
    private Integer code;
    /** 异常栈信息 */
    private String stack;

    public UQException(Integer code, String name, String msg) {
        super(msg);
        this.name = name;
        this.msg = msg;
        this.code = code;
        this.stack = "";
    }

    public UQException(CtrlMethodEnum cmEnum, String msg) {
        super(msg);
        this.name = cmEnum.getMsg();
        this.msg = msg;
        this.code = cmEnum.getCode();
        this.stack = "";
    }

    public UQException(Integer code, String name, String msg, Throwable cause) {
        super(msg, cause);
        this.name = name;
        this.msg = msg;
        this.code = code;
        this.stack = cause.getMessage();
        // getTrace(cause);
    }

    public UQException(CtrlMethodEnum cmEnum, String msg, Throwable cause) {
        super(msg, cause);
        this.name = cmEnum.getMsg();
        this.msg = msg;
        this.code = cmEnum.getCode();
        this.stack = cause.getMessage();
    }

    public String getTrace(Throwable t) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter writer = new PrintWriter(stringWriter);
        t.printStackTrace(writer);
        StringBuffer buffer = stringWriter.getBuffer();
        return buffer.toString();
    }
}
